﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;
using Xenta.Utils;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class RegionTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase("xy", "name")]
        public void Can_CRUD(string code, string name)
        {
            var region = Processor.Execute(new CreateRegion
            {
                Code = code,
                Name = name,
                CountryID = Processor.Execute(new CreateCountry
                {
                    Code = "xy"
                }).Result.EntityID
            }).Result;

            Assert.NotNull(region);
            region.Code.AreEqualIgnoringCase(code);
            region.Name.AreEqualIgnoringCase(name);
            region.AreEqual(Processor.Execute(new GetRegion
            {
                EntityID = region.EntityID
            }).Result);
            region.AreEqual(Processor.Execute(new GetRegionByCode
            {
                Code = code
            }).Result);

            code = code.Reverse();
            name = name.Reverse();
            var upd_region = Processor.Execute(new UpdateRegion
            {
                EntityID = region.EntityID,
                Code = code,
                Name = name,
                CountryID = region.Country.EntityID,
                Flags = RegionFlags.Active
            }).Result;

            Assert.NotNull(upd_region);
            upd_region.AreEqual(region);
            upd_region.Code.AreEqualIgnoringCase(code);
            upd_region.Name.AreEqualIgnoringCase(name);
            upd_region.Flags.AreEqual(RegionFlags.Active);

            Processor.Execute(new DeleteRegion
            {
                EntityID = region.EntityID
            });
        }

        [Test]
        public void Cannot_Create_If_Code_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateRegion
            {
                Code = String.Empty
            }));
        }

        [Test]
        public void Cannot_Update_If_Code_Empty()
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new UpdateRegion
            {
                Code = String.Empty
            }));
        }
    }
}
